//
// Created by win10 on 24-11-29.
//

//基于链式存储的树形数据结构
//一、二叉链式表示法

//1.结构体
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    //1.数据域
    int data;
    //左右孩子指针
    struct Ltree *lchild;
    struct Ltree *rchild;
    //父节点指针
    struct Ltree *parent;
}Ltree;

//2.初始化
Ltree *createLtree(int value) {
    //为子节点分配空间
    Ltree *newNode = (Ltree *) malloc(sizeof(Ltree));
    //鲁棒性判断
    if(newNode==NULL) {
        return NULL;
    }
    //值的分配
    newNode->data = value;
    newNode->lchild = NULL;
    newNode->rchild = NULL;
    newNode->parent = NULL;
    return newNode;
}

//创建树
int main(void) {
    Ltree *nodeA = createLtree(10);
    Ltree *nodeB = createLtree(20);
    Ltree *nodeC = createLtree(30);
    Ltree *nodeD = createLtree(40);
    Ltree *nodeE = createLtree(50);

    //节点的连接
    nodeA->lchild = nodeB;
    nodeA->rchild = nodeC;
    nodeB->lchild = nodeD;
    nodeC->lchild = nodeE;

    nodeB->parent=nodeA;
    nodeC->parent=nodeA;
    nodeD->parent=nodeB;
    nodeE->parent=nodeC;
}